package com.arsl.client.view;

import java.io.UnsupportedEncodingException;

import com.arsl.client.presenter.AddContentPresenter;
import com.arsl.client.presenter.Presenter;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class AddContentView extends Composite implements HasPresenter {

	private static AddContentViewUiBinder uiBinder = GWT
			.create(AddContentViewUiBinder.class);

	interface AddContentViewUiBinder extends UiBinder<Widget, AddContentView> {
	}

	private int publisherId;

	public AddContentView() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	public void setPublisherId(int publisherId) {
		this.publisherId = publisherId;
	}

	@UiField
	ContentInputWidget contentInputField;

	@UiField
	Button btnOk;

	@UiField
	Button btnCancel;

	private AddContentPresenter presenter;

	@UiHandler("btnOk")
	void okClickHandler(ClickEvent event) {
		if (contentInputField.isValid()) {
			presenter.addContent(publisherId, contentInputField.getType(),
					contentInputField.getTitle(),
					contentInputField.getDescription(),
					contentInputField.getUrl());
		} else {
			Window.alert("Incorrect parameters for content");
		}
	}

	@UiHandler("btnCancel")
	void cancelClickHandler(ClickEvent event) {
		presenter.killContainer();
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = (AddContentPresenter) presenter;
	}

}
